import  pymysql
from common.log_tools import log


#数据库连接
def connect():
    try:
        conn = pymysql.connect(user="root",
                        password="",
                        host="192.168.1.",
                        database="ranzhi",
                        port=3306,
                        charset="utf8"
                               )
    except Exception as e:
        log.error(f"数据库打开错误{e}")
    else:
        return conn
#数据库操作
def excute(conn,sql):
    #获取游标
    try:
        r=conn.cursor()
        result = r.execute(sql)
        print(result)
        conn.commit()
        return result
    except Exception as e:
        print(e)
        log.error(f"数据库执行错误{e}")
#数据库断开连接
def disconnect(conn):
    conn.close()

if __name__ == '__main__':
    conn = connect()
    excute(conn,"delete from sys_user where account like 'test%' ")
    disconnect(conn)



